package src.com.hpu.callback;

import java.io.IOException;
import com.hpu.constants.HttpConstants;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import android.os.Handler;
import android.os.Message;

/**
 * 实现了课程业务类的回调接口
 * @author Administrator
 *
 */
public class CourseCallback implements Callback {

	private Handler handler;

	public CourseCallback(Handler handler) {
		this.handler = handler;
	}

	@Override
	public void onFailure(Call arg0, IOException arg1) {
		handler.sendEmptyMessage(HttpConstants.QUERY_COURSE_ERROR);
	}

	@Override
	public void onResponse(Call arg0, Response arg1) throws IOException {
		String result = arg1.body().string();
		if (result!=null&&result.equals("404"))
			handler.sendEmptyMessage(HttpConstants.VPN_TOOLONG);
		else if(result.equals("501")){
			handler.sendEmptyMessage(HttpConstants.KEY_ERROR);
		}
		else if(result.equals("502")){
			handler.sendEmptyMessage(HttpConstants.NEED_INFO);
		}
		else {
			Message message = new Message();
			message.what = HttpConstants.QUERY_COURSE_SUCCESS;
			message.obj = result;
			handler.sendMessage(message);
		}
	}

}
